package HIR.graph;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JPanel;

import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.*;

import HIR.relation.Table;

public class xplotGraph extends JPanel implements  gPanel {
	
	Table  valeurs;
	boolean xyplot =false;
	public xplotGraph(Table t) throws Exception{
		this.valeurs = t;
		
	 this.createPanel();
		
        
      
        

		
	}
	
	
	
	public xplotGraph(Table t, boolean b) throws Exception {
		this.valeurs = t ;
		this.xyplot = b;
		this.createPanel();
		// TODO Auto-generated constructor stub
	}



	public void createPanel() throws Exception
	{

		int nbreC = this.valeurs.getName().size();
		XYSeriesCollection xyDataset = new XYSeriesCollection(); 
		if (this.xyplot) nbreC = nbreC -1;
		
		for (int j =0 ; j < nbreC ; j ++) {
			
			 XYSeries serie  = null;
		
		if (this.xyplot )   serie = new XYSeries(""+  this.valeurs.getName().get(j+1));
		else  serie = new XYSeries(""+  this.valeurs.getName().get(j));
	
		for (int i=0 ; i < valeurs.getCard() ; i ++) {
			Number v=0;
			if (this.xyplot) {
				if ((valeurs.get(i,j+1 )) instanceof String ) {
					v = java.lang.Double.parseDouble((String) valeurs.get(i,j+1 )); 
				}
				else {
					v =(Number)valeurs.get(i,j+1 );
				}
				serie.add((Number)valeurs.get(i, 0),v);
			//	System.out.println((Number)valeurs.get(i,j+1 ) +  " ---- " + (Number)valeurs.get(i, 0));
			}
			else {
				
				if ((valeurs.get(i,j )) instanceof String ) {
					v = java.lang.Double.parseDouble((String) valeurs.get(i,j )); 
				}
				else {
					v =(Number)valeurs.get(i,j );
				}
				
				serie.add(i,v);
		
			}
			}
		xyDataset.addSeries(serie);
		}
	
		JFreeChart chart = ChartFactory.createXYLineChart( 
				"Graph", // chart title 
				"X", // domain axis label 
				"Y", // range axis label 
				xyDataset, // data 
				PlotOrientation.VERTICAL, // orientation 
				true, // include legend 
				true, // tooltips 
				false // urls 
				); 
 
			
		
		//JFreeChart chart = ChartFactory.createXAreaChart("", "", "", xyDataset, PlotOrientation.HORIZONTAL, true, true, true);
		
		//JFreeChart chart =ChartFactory.createXYLineChart("", "", "", xyDataset, PlotOrientation.HORIZONTAL, true, true, true);
		ChartPanel crepart = new ChartPanel(chart);
		//crepart.setSize(100, 100);
		
		this.add(crepart);
		
		
	
	}

	public void decZoom() {
		// TODO Auto-generated method stub
		
	}

	public JPanel getPanel() {
		// TODO Auto-generated method stub
		return null;
	}

	public HIRJToolBar getToolbar() {
		// TODO Auto-generated method stub
		return null;
	}

	public void incZoom() {
		// TODO Auto-generated method stub
		
	}

	public void moveLeft() {
		// TODO Auto-generated method stub
		
	}

	public void moveLeft(int n) {
		// TODO Auto-generated method stub
		
	}

	public void moveRight() {
		// TODO Auto-generated method stub
		
	}

	public void moveRight(int n) {
		// TODO Auto-generated method stub
		
	}
	
	
}
